﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;


class DataFile
{
    //------------------------------------------------------------
    //This decompresses to "decrypt" a file and make it readable by normal people
    public static void EncodeFile(string path)
    {

        //No such file, abort
        if (!File.Exists(path)) return;

        FileStream sourceFile = File.OpenRead(path);
        FileStream destinationFile = File.Create(path + ".enc");

        byte[] buffer = new byte[sourceFile.Length];
        sourceFile.Read(buffer, 0, buffer.Length);

        using (GZipStream output = new GZipStream(destinationFile,
            CompressionMode.Compress))
        {
            Console.WriteLine("CREATING DATAFILE: " + path + " to " + path + ".enc");

            output.Write(buffer, 0, buffer.Length);
        }

        // Close the files.
        sourceFile.Close();
        destinationFile.Close();
    }

    //------------------------------------------------------------
    //This unpacks a specified data file into a temp file
    public static bool UnpackData(string path)
    {

        //Security check
        if (!File.Exists(path)) return false;

        //Decrypt and open file
        FileStream sourceFile = File.OpenRead(path);
        FileStream destinationFile = File.Create(path+".TMP");

        //Because the uncompressed size of the file is unknown, 
        //we are using an arbitrary buffer size.
        byte[] buffer = new byte[65536];  //16-bit

        //"Decrypt" the file
        using (GZipStream input = new GZipStream(sourceFile,
            CompressionMode.Decompress, false))
        {
            destinationFile.Write(buffer, 0, input.Read(buffer, 0, buffer.Length));
        }

        // Close the files.
        sourceFile.Close();
        destinationFile.Close();
        return true;
    }

}
